<!--
 * @Author: hucc
 * @Date: 2021-10-10 18:30:29
 * @LastEditors: hucc
 * @LastEditTime: 2021-10-10 18:51:36
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .output {
            width: 200px;
            height: 100px;
            border-radius: 5px;
            background-color: orangered;
            color: seagreen;
            font-size: 20px;
            margin: 30px 30px;
        }
    </style>
</head>

<body>
    用户名：<input type="text">
    <div class="output"></div><br>
    <button>修改数据</button>
    <script>
        //获取dom节点
        const input = document.querySelector("input");
        const output = document.querySelector(".output");
        const button = document.querySelector("button");
        //获取数据模型
        let data = {
            //username: null
        };
        //第一条线进行绑定 input的输入的值 数据模型可以获取到
        input.addEventListener('input', function() {
                data.username = this.value;
                //output.innerText = this.value;
            }, false)
            //第二条线进行绑定 data里的值改变 input值也发生改变，即渲染到页面上

        //data值改变
        button.addEventListener('click', function() {
                data.username = "lucy"
            }, false)
            //渲染到页面上  首先得监听到值发生改变 再渲染
        Object.defineProperty(data, 'username', {
            set(value1) {
                update(value1)
            }
        })

        function update(value1) {
            input.value = value1;
            output.innerText = value1;
        }
    </script>
</body>

</html>